package com.tbs.web.config;

import com.tbs.common.util.base.BeanUtil;
import com.tbs.web.config.property.WebAuthProperty;
import com.tbs.web.security.authentication.IAuthManager;
import com.tbs.web.security.authentication.center.IAuthenticationCenter;
import com.tbs.web.security.authentication.center.impl.NoImplAuthCenter;
import com.tbs.web.security.authentication.impl.SpringSecurityContextHolderAuthManager;
import com.tbs.web.security.impl.DefaultAuthAccessDeniedHandler;
import com.tbs.web.security.impl.DefaultAuthEntryPoint;
import com.tbs.web.security.impl.TokenAuthenticationProvider;
import com.tbs.web.uitl.AjaxUtil;
import com.tbs.web.uitl.AuthUtil;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.annotation.Resource;

/**
 * 认证配置
 *
 * @author abstergo
 */
@ConditionalOnProperty(prefix = "tbs.web.auth", name = "enable", havingValue = "true")
public class AuthConfig {

    @Resource
    WebAuthProperty webAuthProperty;

    @Bean
    @ConditionalOnMissingBean(AuthenticationEntryPoint.class)
    public AuthenticationEntryPoint authenticationEntryPoint() {
        return new DefaultAuthEntryPoint();
    }

    @Bean

    @ConditionalOnMissingBean(AccessDeniedHandler.class)
    public AccessDeniedHandler accessDeniedHandler() {
        return new DefaultAuthAccessDeniedHandler();
    }

    @Bean

    @ConditionalOnMissingBean(AuthenticationProvider.class)
    AuthenticationProvider authenticationProvider() {
        return new TokenAuthenticationProvider();
    }

    @Bean
    public IAuthenticationCenter authenticationCenter() {
        return BeanUtil.buildBeanFromProperties(() -> {
            return new NoImplAuthCenter();
        }, webAuthProperty.getAuthenticationCenterImpl(), BeanUtil::buildByEmptyArgs);
    }

    @Bean
    AjaxUtil ajaxUtil() {
        return new AjaxUtil();
    }

    @Bean
    AuthUtil authUtil() {
        return new AuthUtil();
    }

    @Bean
    @ConditionalOnMissingBean(IAuthManager.class)
    public IAuthManager authManager() {
        return BeanUtil.buildBeanFromProperties(SpringSecurityContextHolderAuthManager::new,
            webAuthProperty.getAuthManagerImpl(), BeanUtil::buildByEmptyArgs);
    }
}
